/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class atoi {
    public int myAtoi(String s) {
        if (s.isEmpty())
            return 0;
        s = s.trim();
        StringBuilder sb = new StringBuilder();
        int flag = 1;
        if (s.charAt(0) == '-' || s.charAt(0) == '+'){
            if (s.charAt(0) == '-'){
                flag = -1;
            }
            s = s.substring(1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                sb.append(s.charAt(i));
                long l = flag * Long.parseLong(sb.toString());
                if (l >= Integer.MAX_VALUE || l <= Integer.MIN_VALUE) {
                    if (flag == -1) {
                        return Integer.MIN_VALUE;
                    }
                    return Integer.MAX_VALUE;
                }
            }else {
                break;
            }
        }
        if (sb.length() == 0) {
            return 0;
        }
        return flag * Integer.parseInt(sb.toString());
    }
}
